const app = getApp()
const Api = require("../../../utils/api.js")
const ApiRequest = require("../../../utils/request.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    password: null,
    username: null,
    openId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getUsername: function (e) {
    this.setData({
      username: e.detail.value
    })
  },
  getPassword: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  checkEmpty: function (username, password) {
    let result = false
    if (username == null || username == "") {
      result = true
      wx.showToast({
        title: '账号名不能为空！',
        icon: 'none'
      })
    } else if (password == null || password == "") {
      result = true
      wx.showToast({
        title: '密码不能为空！',
        icon: 'none'
      })
    }
    return result
  },
  // 账号密码登录
  loginClick: function (e) {
    let self = this
    let username = this.data.username
    let password = this.data.password
    if (self.checkEmpty(username, password)) {
      return
    }
    var url = Api.login_doctor;
    var params = {
      username: username,
      password: password,
      openId: app.globalData.openid,
    };
    var method = 'POST';
    var header = "application/x-www-form-urlencoded";
    var success = function (res) {
      console.log("doctor login: ", res)

      if (res.code == 200) {
        wx.showToast({
          icon: 'success',
          title: '登录成功',
        })
        app.globalData.loginUser = res.data
        setTimeout(function () {
          wx.redirectTo({
            url: '../../doctor/index/index?doctorInfo=' + res.data,
          })
        }, 1000)

      } else {
        wx.showToast({
          icon: 'none',
          title: '用户名或密码错误！',
        })
      }
    }
    var fail = function (data) {
      wx.showToast({
        icon: 'none',
        title: 'data',
      })
    }
    ApiRequest.request(url, params, method, header, success, fail);
  },
  /**
   * 获取用户openId等信息
   */
  getUserProfile(e) {
    var self = this
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log("wx.getUserProfile: ", res.userInfo)
        // 初始化头像名字动画
        app.globalData.userInfo = res.userInfo;
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
        // 用户login
        self.login(res.userInfo)
      },
      fail: (res) => {
        console.log("wx.getUserProfile fail: ", res)
        wx.showModal({
          title: '提示',
          content: '必须授权才能问诊哦~',
          success: function () {
            console.log("用户点击确定")
          }
        })
      }
    })
  },
  /**
   * 用户登录 || 注册
   */
  login: function (usrInfo) {
    var self = this;
    var url = Api.login_patient;
    var params = {
      openid: app.globalData.openid,
      nickName: usrInfo.nickName,
      gender: usrInfo.gender,
      role: 0
    };
    var method = 'POST';
    var header = "application/x-www-form-urlencoded";
    var success = function (res) {
      if (res.code == 200) {
        wx.showToast({
          icon: 'success',
          title: '登录成功',
        })
        app.globalData.loginUser = res.data
        setTimeout(function () {
          wx.redirectTo({
            url: '../../doctor/index/index?doctorInfo=' + res.data,
          })
        }, 1000)

      } else {
        wx.showToast({
          icon: 'error',
          title: res.msg,
        })
      }
    }
    var fail = function (e) {
      console.log("微信登录失败: ", e)
      wx.showModal({
        title: '微信登录失败',
        content: '发生异常错误，请使用账号密码登录。',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    ApiRequest.request(url, params, method, header, success, fail);
  }
})